<template>
    <div class="login">
        <Navbar title="注册">
            <template>
                <span @touchstart='$router.replace("/login")'>登录</span>
            </template>
        </Navbar>
        <van-form @submit="onRegis">
            <van-field
                v-model.trim="username"
                name="用户名"
                label="用户名"
                placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
                v-model.trim="password"
                type="password"
                name="密码"
                label="密码"
                placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]"
            />
            <van-field
                v-model.trim="repassword"
                type="password"
                name="确认密码"
                label="确认密码"
                placeholder="确认密码"
                :rules="[{ required: true, message: '请填写确认密码' }]"
            />
            <div style="margin: 16px;">
                <van-button round block type="info" native-type="submit">注册</van-button>
            </div>
        </van-form>
    </div>
</template>
<script>
import { 
    Form,
    Field,
    SubmitBar,
    Button,
} from 'vant';
import {Navbar} from '@/components/'
export default {
   data() {
      return {
          username:'',
          password:'',
          repassword:''
      }
   },
   components:{
       [Form.name]:Form,
       [Field.name]:Field,
       [SubmitBar.name]:SubmitBar,
       [Button.name]:Button,
       Navbar,
   },
   computed:{
   },
   methods:{
       onRegis(){
           var data={
               username:this.username,
               password:this.password
           }
           if(this.password==this.repassword){
               this.$http.fetchRegis(data).then(res=>{
                   if(res){
                       this.$toast.success('注册成功!')
                       this.timer=setTimeout(()=>{
                           this.$router.replace('/login')
                       },2000)
                   }
               })
           }else{
               this.$toastt.fail('两次输入的密码不一致！')
           }
       }
   },
   beforeDestroy(){
       clearTimeout(this.timer)
   }
}
</script>
<style lang="scss" scoped>

</style>
